﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.DrugTrade.Web;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.Model.Enum;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;

namespace HZ.DrugTrade.Web.Hospitals.Collection
{
    public partial class SelectInvoice : BasePage
    {
        private static string companyid;
        IBLL_Purchase iPurchase = Factory.BLL_Purchase();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SetBind();
            }
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            SetBind();
        }

        private void SetBind()
        {
            //只有已通过审核的发票才能显示
            //AspNetPager1.PageSize = Constants.GRIDVIEW_PAGE_SIZE;
            try
            {
                companyid=Request.QueryString["companyId"];
                int totalRecord = 0;
                HZ.DrugTrade.Model.Query.Invoice qryWhere = new HZ.DrugTrade.Model.Query.Invoice();
                qryWhere.CompanyID_PS =companyid;
                qryWhere.HospitalID = CurrentAccount.GetUser().Org.OrgID;
                //发票审核状态为1，即通过审核
                qryWhere.AuditStatus = 1;
                qryWhere.SortFields = "InvoiceTime desc";
                qryWhere.InvoiceTimeFrom = new HZ.Common.ConvertHelper(this.txtStartDate.Text).ToDateTime;
                qryWhere.InvoiceTimeTo = new HZ.Common.ConvertHelper(this.txtEndDate.Text).ToDateTime;

               
                IList<HZ.DrugTrade.Model.View.Invoice> lstInvoice = iPurchase.GetInvoiceByCompanyID_PS(qryWhere, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalRecord);
                AspNetPager1.RecordCount = totalRecord;
                gvwDataInfo.AutoGenerateColumns = false;
                this.gvwDataInfo.DataSource = lstInvoice;
                this.gvwDataInfo.DataBind();

            }
            catch (Exception ex)
            {
                PageError(ex.Message, "");
            }

        }


        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            SetBind();
        }

        protected void btnSelect_Click(object sender, EventArgs e)
        {
            //发票号
            string recipitIds = hfdIds.Value;
            //发票流水号
            string sIds = hfdSIds.Value;
            //发票总金额
            string money = allMoney.Value;
            int state = iPurchase.SelectInvoiceStatus(sIds.Substring(0, sIds.Length - 1));
            if (state == 1)
            {
                HZ.Web.WebUtil.Alert("发票单已撤回!", this);
                SetBind();
            }
            else
            {
                HZ.Web.WebUtil.RedirectByScript(string.Format("BulidPaymentAdd.aspx?recipitIds={0}&sIds={1}&money={2}&companyid={3}", recipitIds, sIds.Replace("'", "|"), money, companyid));
            }
        }
    }
}
